package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.DevGpsRealtime;
import com.bdmc.device.domain.bo.DevGpsRealtimeBo;
import com.bdmc.device.domain.vo.DevGpsRealtimeVo;
import com.bdmc.device.mapper.DevGpsRealtimeMapper;
import com.bdmc.device.service.IDevGpsRealtimeService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时位置信息Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-22
 */
@Service
public class DevGpsRealtimeServiceImpl extends ServicePlusImpl<DevGpsRealtimeMapper, DevGpsRealtime, DevGpsRealtimeVo> implements IDevGpsRealtimeService {

    @Override
    public DevGpsRealtimeVo queryById(Long realtimeId){
        return getVoById(realtimeId);
    }

    @Override
    public TableDataInfo<DevGpsRealtimeVo> queryPageList(DevGpsRealtimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevGpsRealtime> lqw = buildQueryWrapper(bo);
        Page<DevGpsRealtimeVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevGpsRealtimeVo> mySelectPage(DevGpsRealtimeBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevGpsRealtimeVo> queryList(DevGpsRealtimeBo bo) {
        LambdaQueryWrapper<DevGpsRealtime> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevGpsRealtime> buildQueryWrapper(DevGpsRealtimeBo bo) {
        LambdaQueryWrapper<DevGpsRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, DevGpsRealtime::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getDevicePhoneNum()), DevGpsRealtime::getDevicePhoneNum, bo.getDevicePhoneNum());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), DevGpsRealtime::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), DevGpsRealtime::getLng, bo.getLng());
        lqw.eq(bo.getDataTime() != null, DevGpsRealtime::getDataTime, bo.getDataTime());
        lqw.eq(bo.getOnlineStatus() != null, DevGpsRealtime::getOnlineStatus, bo.getOnlineStatus());
        lqw.eq(bo.getAlarmCode() != null, DevGpsRealtime::getAlarmCode, bo.getAlarmCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmType()), DevGpsRealtime::getAlarmType, bo.getAlarmType());
        lqw.eq(bo.getHight() != null, DevGpsRealtime::getHight, bo.getHight());
        lqw.eq(bo.getSpeed() != null, DevGpsRealtime::getSpeed, bo.getSpeed());
        lqw.eq(bo.getDirection() != null, DevGpsRealtime::getDirection, bo.getDirection());
        lqw.eq(bo.getMileage() != null, DevGpsRealtime::getMileage, bo.getMileage());
        lqw.eq(StringUtils.isNotBlank(bo.getOilMass()), DevGpsRealtime::getOilMass, bo.getOilMass());
        lqw.eq(bo.getVoltageLevel() != null, DevGpsRealtime::getVoltageLevel, bo.getVoltageLevel());
        lqw.eq(bo.getGsmSignal() != null, DevGpsRealtime::getGsmSignal, bo.getGsmSignal());
        lqw.eq(StringUtils.isNotBlank(bo.getDevState()), DevGpsRealtime::getDevState, bo.getDevState());
        lqw.eq(StringUtils.isNotBlank(bo.getCarLocation()), DevGpsRealtime::getCarLocation, bo.getCarLocation());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevGpsRealtimeBo bo) {
        DevGpsRealtime add = BeanUtil.toBean(bo, DevGpsRealtime.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setRealtimeId(add.getRealtimeId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevGpsRealtimeBo bo) {
        DevGpsRealtime update = BeanUtil.toBean(bo, DevGpsRealtime.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevGpsRealtime entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public List<DevGpsRealtimeVo> selectPersonRealtimeListAll(DevGpsRealtimeBo devGpsRealtimeBo) {
        return baseMapper.selectPersonRealtimeListAll(devGpsRealtimeBo);
    }

    @Override
    public List<DevGpsRealtimeVo> selectCarRealtimeListAll(DevGpsRealtimeBo devGpsRealtimeBo) {
        return baseMapper.selectCarRealtimeListAll(devGpsRealtimeBo);
    }

    @Override
    public List<Map<String, Object>> countHomeWorkPerson(Long deptId) {
        return baseMapper.countHomeWorkPerson(deptId);
    }

    @Override
    public List<Map<String, Object>> countHomeWorkCar(Long deptId) {
        return baseMapper.countHomeWorkCar(deptId);
    }

    @Override
    public Map<String, Object> countHomeAllData() {
        Map<String,Object> map = new HashMap<>();
        map.put("personCount",baseMapper.countPerson());
        map.put("carCount",baseMapper.countCar());
        map.put("facCount",baseMapper.countFac());
        return map;
    }

    @Override
    public DevGpsRealtime getByDeviceId(Long deviceId) {
        LambdaQueryWrapper<DevGpsRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(DevGpsRealtime::getDeviceId,deviceId).last("limit 1");
        return getOne(lqw);
    }

    @Override
    public List<DevGpsRealtimeVo> queryUploadRealtime() {
        return baseMapper.queryUploadRealtime();
    }
}
